﻿using System.Diagnostics;

namespace vztools
{
    public static class Utils
    {
        public static List<string> GetArgsList(string[] args)
        {
            List<string> argsList = new List<string>();
            for (int i = 0; i < args.Length; i++)
            {
                argsList.Add(args[i]);
            }
            return argsList;
        }

        public static void WriteLine(string text, ConsoleColor? foreColor = null, ConsoleColor? backColor = null)
        {
            if (backColor.HasValue)
                Console.BackgroundColor = backColor.Value;
            if (foreColor.HasValue)
                Console.ForegroundColor = foreColor.Value;
            Console.WriteLine(text);
            Console.ResetColor();
        }

        public static void Write(string text, ConsoleColor? foreColor = null, ConsoleColor? backColor = null)
        {
            if (backColor.HasValue)
                Console.BackgroundColor = backColor.Value;
            if (foreColor.HasValue)
                Console.ForegroundColor = foreColor.Value;
            Console.Write(text);
            Console.ResetColor();
        }


        public static void CopyDirectory(string sourceDir, string destinationDir)
        {
            // 检查源目录是否存在
            if (!Directory.Exists(sourceDir))
            {
                return;
            }
            // 如果目标目录不存在，则创建它
            if (!Directory.Exists(destinationDir))
            {
                Directory.CreateDirectory(destinationDir);
            }
            // 获取源目录中的所有文件
            string[] files = Directory.GetFiles(sourceDir);
            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                string destinationFilePath = Path.Combine(destinationDir, fileName);
                // 复制文件
                File.Copy(file, destinationFilePath, true);
            }
            // 获取源目录中的所有子目录
            string[] subDirectories = Directory.GetDirectories(sourceDir);
            foreach (string subDirectory in subDirectories)
            {
                string subDirectoryName = Path.GetFileName(subDirectory);
                string destinationSubDirectory = Path.Combine(destinationDir, subDirectoryName);
                // 递归调用 CopyDirectory 方法复制子目录
                CopyDirectory(subDirectory, destinationSubDirectory);
            }
        }

        public static void ExecuteScriptProcess(string executor, string argStr)
        {
            // 创建 ProcessStartInfo 对象
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = executor;
            // 设置要执行的 PowerShell 命令
            startInfo.Arguments = argStr;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = false;
            // startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            // 创建 Process 对象
            using (Process process = new Process())
            {
                process.StartInfo = startInfo;
                // 启动进程
                process.Start();
                // 获取命令执行结果
                // string output = process.StandardOutput.ReadToEnd();
                string error = process.StandardError.ReadToEnd();
                // // 等待进程执行完毕
                process.WaitForExit();
                // if (!string.IsNullOrEmpty(output))
                // {
                //     Console.WriteLine("命令执行结果：");
                //     Console.WriteLine(output);
                // }
                if (!string.IsNullOrEmpty(error))
                {
                    Console.WriteLine("命令执行错误：");
                    Console.WriteLine(error);
                }
            }
        }

    }
}
